﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace OLAP.Common
{
    public class ClientSetting
    {
        /// <summary>
        /// Used to Convert a .NET date format to a PHP date format, that Dynarch Calendar uses.
        /// </summary>
        /// <param name="netPattern">The .NET custom date pattern to be converted</param>
        /// <returns>The proper PHP pattern</returns>
        /// <remarks>Can't handle general markers like "d" or "D" the inputed pattern needs to always be a custom date pattern!</remarks>
        public static string ConvertDatePatternFromNET2PHP(string netPattern)
        {
            StringBuilder marker = new StringBuilder(),
                ret = new StringBuilder();
            int i;
            char c, markerType = '\0';


            for (i = 0; i < netPattern.Length; i++)
            {
                c = netPattern[i];

                if (c != markerType && markerType != '\0')
                {
                    ret.Append(GetPHPMarkerFromNETMarker(marker.ToString()));
                    markerType = '\0';
                    marker = new StringBuilder();
                }

                switch (c)
                {
                    case 'd':
                        markerType = 'd';
                        marker.Append(markerType);
                        break;
                    case 'M':
                        markerType = 'M';
                        marker.Append(markerType);
                        break;
                    case 'y':
                        markerType = 'y';
                        marker.Append(markerType);
                        break;
                    case 'm':
                        markerType = 'm';
                        marker.Append(markerType);
                        break;
                    case 'H':
                        markerType = 'H';
                        marker.Append(markerType);
                        break;
                    case 'h':
                        markerType = 'h';
                        marker.Append(markerType);
                        break;
                    case 't':
                        markerType = 't';
                        marker.Append(markerType);
                        break;
                    default:
                        ret.Append(c);
                        break;
                }
            }

            if (marker.Length > 0)
            {
                ret.Append(GetPHPMarkerFromNETMarker(marker.ToString()));
            }

            return ret.ToString();
        }

        /// <summary>
        /// Used to convert individual .NET custom date marker to their PHP counterparts.
        /// </summary>
        /// <param name="netMarket">The .NET custom date marter to be converted</param>
        /// <returns>The PHP date formats counterpart to the inputed .NET marker</returns>
        private static string GetPHPMarkerFromNETMarker(string netMarket)
        {

            string phpMarker = String.Empty;
            switch (netMarket)
            {
                case "dddd": phpMarker = "%A"; break;
                case "ddd": phpMarker = "%a"; break;
                case "dd": phpMarker = "%d"; break;
                case "d": phpMarker = "%e"; break;

                case "MMMM": phpMarker = "%B"; break;
                case "MMM": phpMarker = "%b"; break;
                case "MM": phpMarker = "%m"; break;
                case "M": phpMarker = "%o"; break;

                case "yyyy": phpMarker = "%Y"; break;
                case "yy": phpMarker = "%y"; break;
                case "y": phpMarker = "%y"; break;

                case "mm": phpMarker = "%M"; break;
                case "m": phpMarker = "%M"; break;

                case "HH": phpMarker = "%H"; break;
                case "H": phpMarker = "%k"; break;

                case "hh": phpMarker = "%I"; break;
                case "h": phpMarker = "%l"; break;

                case "t": phpMarker = "%p"; break;
                case "tt": phpMarker = "%p"; break;

                default:
                    phpMarker = netMarket;
                    break;
            }

            return phpMarker;
        }
    }
}